using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace DBSourceToolsLib.SysObjects.UI.Forms
{
    public partial class DatabasePropertiesUC : UserControl
    {
        private SourceDatabase m_SourceDatabase;
        private DatabaseEx m_DatabaseEx;
        private bool m_IsNew;

        public DatabasePropertiesUC(DatabaseEx deployDatabase, bool isNew)
        {
            InitializeComponent();
            this.m_DatabaseEx = deployDatabase;
            this.m_IsNew = isNew;
        }

        private void DatabasePropertiesUC_Load(object sender, EventArgs e)
        {
            this.m_textBoxNickName.Text = m_DatabaseEx.NickName;
            this.m_textBoxServerName.Text = m_DatabaseEx.DataSourceName;
            this.m_textBoxDatabaseName.Text = m_DatabaseEx.DatabaseName;
            if (!String.IsNullOrEmpty(this.m_DatabaseEx.UserName))
            {
                this.m_textBoxUserName.Text = this.m_DatabaseEx.UserName;
                this.m_textBoxPassword.Text = this.m_DatabaseEx.Password;
                this.m_comboBoxAuthentication.SelectedIndex = 1;
                EnableButtons(1);
            }
            else
            {
                this.m_comboBoxAuthentication.SelectedIndex = 0;
                EnableButtons(0);
            }
            if (this.m_DatabaseEx.IsDeploymentTarget)
            {
                this.m_panelOptions.Visible = false;
            }
            this.m_b_Data.Checked = this.m_SourceDatabase.ScriptOptions.ScriptData;
            this.m_b_DropAndCreate.Checked = this.m_SourceDatabase.ScriptOptions.ScriptDropAndCreate;
            this.m_b_ScriptViews.Checked = this.m_SourceDatabase.ScriptOptions.ScriptViews;
            this.m_b_StoredProcs.Checked = this.m_SourceDatabase.ScriptOptions.ScriptStoredProcs;
            this.m_b_Users.Checked = this.m_SourceDatabase.ScriptOptions.ScriptUsers;
            this.m_b_Tables.Checked = this.m_SourceDatabase.ScriptOptions.ScriptTables;
        }

        private void EnableButtons(int selIdx)
        {
            if (selIdx == 0)
            {
                this.m_textBoxPassword.Enabled = false;
                this.m_textBoxUserName.Enabled = false;
            }
            else
            {
                this.m_textBoxPassword.Enabled = true;
                this.m_textBoxUserName.Enabled = true;
            }
            if (!m_IsNew)
            {
                this.m_textBoxDatabaseName.ReadOnly = true;

                this.m_textBoxNickName.ReadOnly = true;
                this.m_textBoxPassword.ReadOnly = true;
                this.m_textBoxServerName.ReadOnly = true;
                this.m_textBoxUserName.ReadOnly = true;
                this.m_comboBoxAuthentication.Enabled = false;
            }
        }



        private void m_btnProcOptions_Click(object sender, EventArgs e)
        {
            DBSourceToolsLib.UI.Forms.StoredProcOptionsForm oTableOptionsForm
                = new DBSourceToolsLib.UI.Forms.StoredProcOptionsForm(
                    this.m_SourceDatabase.NickName, this.m_DatabaseEx.BaseDirectory + @"\StoredProcScriptOptions.xml"
                    );

            oTableOptionsForm.ShowDialog(this);
            ReloadScriptOptions();

        }

        private void m_btnTableOptions_Click(object sender, EventArgs e)
        {

            DBSourceToolsLib.UI.Forms.TableOptionsForm oTableOptionsForm
                = new DBSourceToolsLib.UI.Forms.TableOptionsForm(
                    this.m_SourceDatabase.NickName, this.m_DatabaseEx.BaseDirectory + @"\TableScriptOptions.xml"
                    );

            oTableOptionsForm.ShowDialog(this);
            ReloadScriptOptions();
        }


        public void SetSourceDatabase(SourceDatabase databaseEx)
        {
            this.m_SourceDatabase = databaseEx;
        }

        private void m_btnViewOptions_Click(object sender, EventArgs e)
        {
            DBSourceToolsLib.UI.Forms.ViewOptionsForm oTableOptionsForm
                = new DBSourceToolsLib.UI.Forms.ViewOptionsForm(
                    this.m_SourceDatabase.NickName, this.m_DatabaseEx.BaseDirectory + @"\ViewScriptOptions.xml"
                    );

            oTableOptionsForm.ShowDialog(this);
            ReloadScriptOptions();
        }

        public void Save()
        {
            this.m_SourceDatabase.ScriptOptions.ScriptData = this.m_b_Data.Checked;
            this.m_SourceDatabase.ScriptOptions.ScriptDropAndCreate = this.m_b_DropAndCreate.Checked;
            this.m_SourceDatabase.ScriptOptions.ScriptStoredProcs = this.m_b_StoredProcs.Checked;
            this.m_SourceDatabase.ScriptOptions.ScriptTables = this.m_b_Tables.Checked;
            this.m_SourceDatabase.ScriptOptions.ScriptViews = this.m_b_ScriptViews.Checked;
            this.m_SourceDatabase.ScriptOptions.ScriptUsers = this.m_b_Users.Checked;
            this.m_SourceDatabase.ScriptOptions.WriteToDisk();
            ReloadScriptOptions();
        }

        private void m_btnUsers_Click(object sender, EventArgs e)
        {
            DBSourceToolsLib.SysObjects.UI.Forms.UserOptionsForm oUserForm
                = new UserOptionsForm(this.m_SourceDatabase.NickName, this.m_DatabaseEx.BaseDirectory + @"\UserScriptOptions.xml");
            oUserForm.ShowDialog(this);
            ReloadScriptOptions();

        }

        private void ReloadScriptOptions()
        {
            this.m_SourceDatabase.ScriptOptions.LoadScriptOptionsFromDisk(null);
            //throw new NotImplementedException();
        }

        private void OnOptionClick(object sender, EventArgs e)
        {
            Save();
        }
    }
}
